<?php
namespace app\web\controller;
use app\web\model\LineMemberLevelModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 线下会员等级管理
 */
class LineMemberLevelController extends StoreBaseController
{   
	protected $model;
	protected $LineMemberlevelMode;
	
	protected function _initialize() {
        parent::_initialize();
        }
	
    /**
     * 线下会员等级首页
     * @author [] 
     */
    public function index( $page = 1){
		$list = db("LineMemberlevel")->select();
		return $this->fetch('index', ['list' => $list]);
	}
	/**
     * 新增会员等级
     * @author [] 
     */
    public function add(){
		$agent_id=session('storeInfo')['id'];
		$data['levelname']=input("name");
		$data['upgrade']=input("upgrade");
		$data['agent_id'] = $agent_id;
		$data['update_time']=date('Y-m-d H:i:s',time());
		$res=db('LineMemberlevel')->insert($data);
		if($res){
			$cache=cache('member_level');
			$id=db('LineMemberlevel')->getLastInsID();
			$arr=array("id"=>$id,"levelname"=>input("name"),"upgrade"=>input("upgrade"));
			$cache[$agent_id][$id]=$arr;
			cache("member_level",$cache);
			return json(['code'=>200,'msg'=>"新增会员等级成功"]);
		}else{
			return json(['code'=>408,'msg'=>"新增失败"]);
		}
	}
	/**
     * [edit 编辑等级名称]
     * @return [type] [description]
     * @author []
     */
    public function edit()
    {
    	$agent_id=session('storeInfo')['id'];  
		$id=input('id');   	
		$data['id']=$id;	
		$data['levelname']=input('name');
		$data['upgrade']=input("upgrade");
		$data['update_time']=date('Y-m-d H:i:s',time());
		$res=db('LineMemberlevel')->update($data);
		if($res){
			$cache=cache('member_level');
			$arr=array("id"=>$id,"levelname"=>input("name"),"upgrade"=>input("upgrade"));
			$cache[$agent_id][$id]=$arr;
			cache("member_level",$cache);
			return json(['code'=>200,'msg'=>"修改成功"]);
		}else{
			return json(['code'=>408,'msg'=>"修改失败"]);
		}
          	
    }
    
    /**
     * [edit 编辑折扣]
     * @return [type] [description]
     * @author []
     */
    public function edit_zhekou()
    {     	
		$data['id']=input('id');	
		$data['discount']=input('name');
		$data['update_time']=date('Y-m-d H:i:s',time());
		$res=db('LineMemberlevel')->update($data);
		if($res){
			return json(['code'=>200,'msg'=>"修改成功"]);
		}else{
			return json(['code'=>408,'msg'=>"修改失败"]);
		}
          	
    }
    
     /**
     * [del删除会员等级]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if (db('LineMemberlevel')->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
